# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None
from collections import deque
class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        a = deque()
        b = deque()
        while l1:
            a.appendleft(l1.val)
            l1 = l1.next
        while l2:
            b.appendleft(l2.val)
            l2 = l2.next
        s = 0
        for i in range(len(a)):
            s += a[i] * pow(10, i )
        s1 = 0
        for i in range(len(b)):
            s1 += b[i] * pow(10, i )
        s += s1
        s = str(s)
        n = len(s)
        head = ListNode(-1)
        cur = head
        for i in range(n):
            a = ListNode(s[i])
            head.next = a
            head = head.next
        return cur.next



l1 = ListNode(7)
l1.next = ListNode(2)
l1.next.next = ListNode(4)
l1.next.next.next = ListNode(3)

l2 = ListNode(5)
l2.next = ListNode(6)
l2.next.next = ListNode(4)

m = Solution()
s = m.addTwoNumbers(l1,l2)

while s!= None:
    print(s.val)
    s = s.next
